import {
  CarpoolTypeCreat,
  CarpoolTypeUpdate,
} from '../interface/params.interface.ts'
import {
  toSnakeCase,
  backResult,
  selectResultsHandle,
} from '../utils/common.ts'
import CarpoolType from '../model/carpooltype.model.ts'

class CarpoolTypeService {
  // 查询拼车类型
  async selectCarpoolType(carpoolTypes: string[]) {
    const res: any = await CarpoolType.findAll({
      where: {
        carpool_type: {
          $or: carpoolTypes,
        },
      },
    })
    return selectResultsHandle(res)
  }

  // 创建拼车类型
  async inseretCarpoolType(obj: CarpoolTypeCreat) {
    const option = toSnakeCase(obj)
    const res: any = await CarpoolType.create(option)
    return res ? res.dataValues : null
  }

  // 更新拼车类型
  async updateCarpoolType(obj: CarpoolTypeUpdate) {
    const { carpool_type_id, ...option } = toSnakeCase(obj)
    const res: any = await CarpoolType.update(option, {
      where: { carpool_type_id },
    })
    return backResult(res)
  }

  // 删除拼车类型
  async deleteCarpoolType(obj: { carpoolTypeId: string }) {
    const { carpool_type_id } = toSnakeCase(obj)
    const res: number = await CarpoolType.destroy({
      where: { carpool_type_id },
    })
    return backResult(res)
  }
}

const carpoolTypeService = new CarpoolTypeService()
export const {
  selectCarpoolType,
  inseretCarpoolType,
  updateCarpoolType,
  deleteCarpoolType,
} = carpoolTypeService
export default carpoolTypeService
